package org.dromara.third.convert.gasAnalysis.validator;

import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.convert.gasAnalysis.dto.GasAnalysisDTO;
import org.dromara.third.convert.util.ExistenceValidator;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class GasAnalysisValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return GasAnalysisDTO.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        GasAnalysisDTO gasAnalysisDTO = (GasAnalysisDTO) target;

        // 基础字段验证
        validateRequiredFields(gasAnalysisDTO, errors);

        // 存在性验证
        validateExistence(gasAnalysisDTO, errors);
    }

    /**
     * 验证必填字段
     */
    private void validateRequiredFields(GasAnalysisDTO dto, Errors errors) {
        ValidateUtil.rejectIfNull(dto::getDataId, String.valueOf(ExceptionEnum.AuthException01.getCode()), "主键不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getZyid, String.valueOf(ExceptionEnum.AuthException01.getCode()), "作业ID不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getZylx, String.valueOf(ExceptionEnum.AuthException01.getCode()), "关联作业类型不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getQtlx, String.valueOf(ExceptionEnum.AuthException01.getCode()), "气体类型不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getQtmc, String.valueOf(ExceptionEnum.AuthException01.getCode()), "代表性气体名称不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getFxjg, String.valueOf(ExceptionEnum.AuthException01.getCode()), "分析结果不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getJgdw, String.valueOf(ExceptionEnum.AuthException01.getCode()), "分析结果单位不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getFxr, String.valueOf(ExceptionEnum.AuthException01.getCode()), "分析人不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getFxsj, String.valueOf(ExceptionEnum.AuthException01.getCode()), "分析时间不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getCyfxbw, String.valueOf(ExceptionEnum.AuthException01.getCode()), "采样分析部位不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getCash, String.valueOf(ExceptionEnum.AuthException01.getCode()), "气体CAS号不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getJcsfhg, String.valueOf(ExceptionEnum.AuthException01.getCode()), "检测是否合格不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getQmtp, String.valueOf(ExceptionEnum.AuthException01.getCode()), "签名图片不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getDelFlag, String.valueOf(ExceptionEnum.AuthException01.getCode()), "删除标志不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getCreateDept, String.valueOf(ExceptionEnum.AuthException01.getCode()), "所属部门不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getCreateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建人不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getCreateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建时间不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getUpdateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新人不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getUpdateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新时间不能为空", errors);
    }

    /**
     * 验证存在性
     */
    private void validateExistence(GasAnalysisDTO dto, Errors errors) {
        // 验证所属部门存在
        ExistenceValidator.validateDeptExists(dto.getCreateDept(), "所属部门", errors);

        // 验证创建人存在
        ExistenceValidator.validateUserExists(dto.getCreateBy(), "创建人", errors);

        // 验证更新人存在
        ExistenceValidator.validateUserExists(dto.getUpdateBy(), "更新人", errors);
    }
}
